﻿using Google.Api.Maps.Service;
using Google.Api.Maps.Service.Geocoding;
using HelloWorld.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace HelloWorld
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "HelloWorld" in both code and config file together.
    public class HelloWorld : IHelloWorld
    {

        public string JoinString(string s1, string s2)
        {           
            return s1 + s2;            
        }


        public string SearchLocation(string sInput)
        {
            var request = new GeocodingRequest();
            request.Address = sInput;
            request.Sensor = "false";
            var response = GeocodingService.GetResponse(request);

            if (response.Status == ServiceResponseStatus.Ok)
            {
                
                GeocodingResult result = response.Results[0];
                if (result != null)
                {
                    return result.Components[0].ToString();
                    //return result.FormattedAddress;
                }
            }
            return "Not Found";
        }

        public string GetDistance(string input1, string input2)
        {

            var request = new GeocodingRequest();
            request.Address = input1;
            request.Sensor = "false";
            var response = GeocodingService.GetResponse(request);

            GeocodingResult result1;
            if (response.Status == ServiceResponseStatus.Ok)
            {

                result1 = response.Results[0];
                
            }

            request.Address = input2;
            request.Sensor = "false";
            response = GeocodingService.GetResponse(request);

            GeocodingResult result2;
            if (response.Status == ServiceResponseStatus.Ok)
            {

                result2 = response.Results[0];

            }

            return "0";
        }


        public int getIdFromUser(string user, string pass)
        {
            MapDBEntities db = new MapDBEntities();

            UserTable user0 = (from p in db.UserTables select p).Where(p => p.Id == 0).First<UserTable>();

            
            if (user0 != null)
                return user0.Id;
            return -1;

        }
    }
}
